#ifndef TC_ONBALANCEVOLUME_HPP
#define TC_ONBALANCEVOLUME_HPP

/** \file OnBalanceVolume.hpp
 * Definition of the function template OnBalanceVolume.
 *
 * (C) Copyright 2003-2006 by Semih Cemiloglu
 * All rights reserved, see COPYRIGHT file for details.
 *
 * $Id: OnBalanceVolume.hpp 110 2006-06-19 03:38:33Z semihc $
 *
 *
 */


#include "TA.hh"

namespace TC {

  //! On Balance Volume function
  TC_EXPORT template <typename InputIterator,
                      typename OutputIterator,
                      class Adaptor>
  void OnBalanceVolume(InputIterator fromPos,
		       InputIterator fromEnd,
		       OutputIterator toPos,
		       const Adaptor adaptor)
  {
    typedef typename Adaptor::Price_t Price_t;

    // Previous/Current OBV value
    register TAfp_t obv = 0.0;
    // Previous close
    register Price_t prevCls = adaptor.cls(*fromPos++);
    // Current period close
    register Price_t cls;

    // First OBV is to be zero
    *toPos++ = obv;

    for(; fromPos!=fromEnd; ++fromPos) {
      // Get current close
      cls = adaptor.cls(*fromPos);
      // If current close is lower than previous
      if(cls < prevCls)
	obv -= adaptor.vol(*fromPos);
      // If current close is higher than previous
      if(cls > prevCls)
	obv += adaptor.vol(*fromPos);
      // Do nothing if the current and previous price is same
      prevCls = cls;

      // Store the OBV value
      *toPos++ = obv;
    } // end for

  }

} // end namespace


#endif /* Include guard */
